package org.jeecg.modules.online.cgform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.online.cgform.converter.ConvertUtil;
import org.jeecg.modules.online.cgform.entity.OnlCgformField;
import org.jeecg.modules.online.cgform.entity.OnlCgformHead;
import org.jeecg.modules.online.cgform.enums.EnhanceDataEnum;
import org.jeecg.modules.online.cgform.mapper.OnlCgformFieldMapper;
import org.jeecg.modules.online.cgform.service.IOnlCgformHeadService;
import org.jeecg.modules.online.cgform.service.IOnlCgformSqlService;
import org.jeecg.modules.online.cgform.utils.CgformUtil;
import org.jeecg.modules.online.cgform.utils.OnlineImportValidator;
import org.jeecg.modules.online.config.exception.BusinessException;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("onlCgformSqlServiceImpl")
/* renamed from: org.jeecg.modules.online.cgform.service.a.f */
/* loaded from: hibernate-re-3.7.1-RC.jar:org/jeecg/modules/online/cgform/service/a/f.class */
public class OnlCgformSqlServiceImpl implements IOnlCgformSqlService {

    /* renamed from: a */
    private static final Logger f458a = LoggerFactory.getLogger(OnlCgformSqlServiceImpl.class);
    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;
    @Autowired
    private IOnlCgformHeadService onlCgformHeadService;

    @Override // org.jeecg.modules.online.cgform.service.IOnlCgformSqlService
    public void saveBatchOnlineTable(OnlCgformHead head, List<OnlCgformField> fieldList, List<Map<String, Object>> dataList) throws BusinessException {
        SqlSession sqlSession = null;
        int i = 0;
        try {
            try {
                ConvertUtil.m398a(2, dataList, fieldList);
                sqlSession = this.sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
                OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) sqlSession.getMapper(OnlCgformFieldMapper.class);
                if (1000 >= dataList.size()) {
                    int i2 = 0;
                    while (i2 < dataList.size()) {
                        m186a(JSON.toJSONString(dataList.get(i2)), head, fieldList, onlCgformFieldMapper);
                        i2++;
                        i++;
                    }
                } else {
                    int i3 = 0;
                    while (i3 < dataList.size()) {
                        m186a(JSON.toJSONString(dataList.get(i3)), head, fieldList, onlCgformFieldMapper);
                        if (i3 % 1000 == 0) {
                            sqlSession.commit();
                            sqlSession.clearCache();
                        }
                        i3++;
                        i++;
                    }
                }
                sqlSession.commit();
                sqlSession.close();
            } catch (Exception e) {
                sqlSession.rollback();
                throw new BusinessException("第" + i + "行" + m185a(e));
            }
        } catch (Throwable th) {
            sqlSession.close();
            throw th;
        }
    }

    /* renamed from: a */
    private String m185a(Throwable th) {
        if (th.getCause() != null) {
            return m185a(th.getCause());
        }
        return th.getMessage();
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlCgformSqlService
    public void saveOrUpdateSubData(String subDataJsonStr, OnlCgformHead head, List<OnlCgformField> subFiledList) throws BusinessException {
        m186a(subDataJsonStr, head, subFiledList, (OnlCgformFieldMapper) SpringContextUtils.getBean(OnlCgformFieldMapper.class));
    }

    @Override // org.jeecg.modules.online.cgform.service.IOnlCgformSqlService
    public Map<String, String> saveOnlineImportDataWithValidate(OnlCgformHead head, List<OnlCgformField> fieldList, List<Map<String, Object>> dataList) {
        String m187a;
        StringBuffer stringBuffer = new StringBuffer();
        OnlineImportValidator onlineImportValidator = new OnlineImportValidator(fieldList);
        OnlCgformFieldMapper onlCgformFieldMapper = (OnlCgformFieldMapper) SpringContextUtils.getBean(OnlCgformFieldMapper.class);
        int i = 0;
        int i2 = 0;
        ConvertUtil.m398a(2, dataList, fieldList);
        int size = dataList.size();
        for (int i3 = 0; i3 < size; i3++) {
            String jSONString = JSON.toJSONString(dataList.get(i3));
            i++;
            String m281a = onlineImportValidator.m281a(jSONString, i);
            if (m281a == null) {
                try {
                    m186a(jSONString, head, fieldList, onlCgformFieldMapper);
                } catch (Exception e) {
                    i2++;
                    if (e.getCause() != null) {
                        m187a = m187a(e.getCause().getMessage());
                    } else {
                        m187a = m187a(e.getMessage());
                    }
                    stringBuffer.append(OnlineImportValidator.m280b(m187a, i));
                }
            } else {
                i2++;
                stringBuffer.append(m281a);
            }
        }
        HashMap hashMap = new HashMap(5);
        hashMap.put(OnlineImportValidator.f343a, stringBuffer.toString());
        hashMap.put(OnlineImportValidator.f344b, OnlineImportValidator.m282a(size, i2));
        return hashMap;
    }

    /* renamed from: a */
    private void m186a(String str, OnlCgformHead onlCgformHead, List<OnlCgformField> list, OnlCgformFieldMapper onlCgformFieldMapper) throws BusinessException {
        JSONObject parseObject = JSONObject.parseObject(str);
        EnhanceDataEnum executeEnhanceImport = this.onlCgformHeadService.executeEnhanceImport(onlCgformHead, parseObject);
        String tableName = onlCgformHead.getTableName();
        if (EnhanceDataEnum.INSERT == executeEnhanceImport) {
            onlCgformFieldMapper.executeInsertSQL(CgformUtil.m372a(tableName, list, parseObject));
        } else if (EnhanceDataEnum.UPDATE == executeEnhanceImport) {
            onlCgformFieldMapper.executeUpdatetSQL(CgformUtil.m341b(tableName, list, parseObject));
        } else {
            if (EnhanceDataEnum.ABANDON == executeEnhanceImport) {
            }
        }
    }

    /* renamed from: a */
    private String m187a(String str) {
        Matcher matcher = Pattern.compile("^Duplicate entry \\'(.*)\\' for key .*$").matcher(str);
        if (matcher.find()) {
            return "重复数据" + matcher.group(1);
        }
        return str;
    }
}
